package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.android.weblog.adapter.PassageListAdapter;
import cn.android.weblog.api.CatagoryAPI;
import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.redisdata.CURRENT;

public class CatagoryPassageActivity extends AppCompatActivity {

    private List<Passage> passage;
    private CatagoryAPI catagoryAPI;
    private PassageListAdapter adapter;
    private ListView lv;
    private TextView title;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catagory_passage);

        catagoryAPI=new CatagoryAPI();
        adapter=new PassageListAdapter(getApplicationContext());
        lv=(ListView)findViewById(R.id.catagory_lv);
        title=(TextView)findViewById(R.id.catagory_title);
        back=(ImageView)findViewById(R.id.catagory_back);

        // 查找当前分类下的文章
        init();

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 单击进入文章详情页
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 将选中的文章信息存入缓存域中
                CURRENT.PASSAGE=(Passage)adapter.getItem(position);
                Intent intent=new Intent(CatagoryPassageActivity.this,PassageDetailActivity.class);
                startActivity(intent);
            }
        });


    }

    /**
     * 初始化页面
     */
    private void init(){
        passage=catagoryAPI.findPassageByCatagoryId(CURRENT.CATAGORY.getCatagoryId());
        adapter.setData(passage);
        lv.setAdapter(adapter);
        title.setText(CURRENT.CATAGORY.getCatagoryName());
    }

}
